function [] = mn_createNetcdf(x_data, y_data, z_data, nc_fname, paralist)
%function to create and write netcdf file with given parameters

x_data(isnan(x_data)) = 99999.9;
x_data = x_data*10;
y_data(isnan(y_data)) = 99999.9;
y_data = y_data*10;
z_data(isnan(z_data)) = 99999.9;
z_data = z_data*10;

ncid = netcdf_create(nc_fname,'NC_NOCLOBBER');

% Create Global Attributes
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'title', paralist.title);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'Summary', paralist.summary);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'keywords', paralist.keywords);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'id', paralist.id);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'history', paralist.history);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'date_created', paralist.date_created);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'creator_name', paralist.creator_name);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'creator_email', paralist.creator_email);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'creator_url', paralist.creator_url);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'institution', paralist.institution);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'project', paralist.project);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'processing_level', paralist.processing_level );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'acknowledgment', paralist.acknowledgment );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'station_name', paralist.station_name);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'station_institution', paralist.station_institution );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'station_institution_url',paralist.station_institution_url );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'station_institution_email', paralist.station_institution_email );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'geospatial_lat', single(paralist.geospatial_lat));
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'geospatial_lon', single(paralist.geospatial_lon));
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'geospatial_alt', single(paralist.geospatial_alt) );
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'time_coverage_start_unix_sec_since_1970_01_01', paralist.time_coverage_start_unix_sec_since_1970_01_01);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'time_coverage_end_unix_sec_since_1970_01_01', paralist.time_coverage_end_unix_sec_since_1970_01_01);
netcdf_putAtt(ncid,netcdf_getConstant('NC_GLOBAL'), 'time_coverage_resolution_sec', paralist.time_coverage_resolution_sec);

data_time_axis_dim = netcdf_defDim(ncid,'data_time_axis_dim', paralist.dimlen);

X_ID = netcdf_defVar(ncid,'Magnetic_Field_X','int', data_time_axis_dim);
Y_ID = netcdf_defVar(ncid,'Magnetic_Field_Y','int', data_time_axis_dim);
Z_ID = netcdf_defVar(ncid,'Magnetic_Field_Z','int', data_time_axis_dim);

netcdf_putAtt(ncid,X_ID,'_FillValue',int32(999999));
netcdf_putAtt(ncid,X_ID,'Unit','nT * 10');
netcdf_putAtt(ncid,Y_ID,'_FillValue',int32(999999));
netcdf_putAtt(ncid,Y_ID,'Unit','nT * 10');
netcdf_putAtt(ncid,Z_ID,'_FillValue',int32(999999));
netcdf_putAtt(ncid,Z_ID,'Unit','nT * 10');

netcdf_endDef(ncid);

netcdf_putVar(ncid, X_ID, x_data);
netcdf_putVar(ncid, Y_ID, y_data);
netcdf_putVar(ncid, Z_ID, z_data);

netcdf_close(ncid);

return;
